const Koa = require('koa')
const router = require('koa-router')()
const session = require('koa-session')
const mongoose = require('mongoose')
const bodyParser = require('koa-bodyparser')
const app = new Koa()

// 配置session中间件
app.keys = ['some secret hurr']
const CONFIG = {
  key: 'koa:sess',
  maxAge: 864000,
  overwrite: true,
  httpOnly: true,
  signed: true,
  rolling: true,
  renew: false
}
app.use(session(CONFIG, app))

// 配置post请求接受参数中间件
app.use(bodyParser())

// 配置路由
let blog = require('./routes/blog')
router.use('/blog', blog)
app.use(router.routes())
app.use(router.allowedMethods())


//连接数据库并且启动服务
//mongoose.Promise = global.Promise
/*mongoose.connect('mongodb://blog:mm19900326@127.0.0.1/blog',{ useMongoClient: true }, function (err) {
    if (err) {
        console.log('数据库连接错误');
    } else {
        app.listen(80, function () {
            console.log('数据库连接成功，启动成功：端口为3000');
        });
    }
});*/
mongoose.connect('mongodb://localhost:27017/blog',{useNewUrlParser: true}).then(() => {
  app.listen(3000, function () {
    console.log('数据库连接成功, http://localhost:3000');
  })
})
